//
//  MTLiveSearchUserController.m
//  MeToo
//
//  Created by 俊松何 on 2018/9/28.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLiveSearchUserController.h"
#import "UISearchBar+Clear.h"
#import "MTAddManagerCell.h"
@interface MTLiveSearchUserController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,MTAddManagerCellDelegate>

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@property (weak, nonatomic) IBOutlet UIButton *cancleButton;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@property(nonatomic,strong) NSMutableArray *resultsSource;

@property(nonatomic,assign) int page;

@end

@implementation MTLiveSearchUserController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchBar.delegate = self;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.searchBar clearBackgroundColor];
    [self.searchBar becomeFirstResponder];
    [self.cancleButton setTitle:NSLTLocalizedString(@"main.cancle", nil) forState:UIControlStateNormal];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.searchBar.placeholder = @"请输入昵称搜索";
    [self.tableView registerNib:[UINib nibWithNibName:@"MTAddManagerCell" bundle:nil] forCellReuseIdentifier:@"MTAddManagerCellID"];
    self.page = 1;
    @weakify(self);
    // 上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.page += 1;
        [self getDataIsHeaderRefresh:NO];
    }];
}


#pragma mark - 请求所有数据方法
- (void)getDataIsHeaderRefresh:(BOOL)headerRefresh {
    
    [[MTUserApiManager manager] searchUserWithKeyWord:self.searchBar.text page:self.page completion:^(MTUserTotalModel *totalUser, MTError *mterror) {
        if(mterror == nil)
        {
            if (headerRefresh) {
                self.resultsSource = totalUser.list;
                [self.tableView reloadData];
            } else {
                if(totalUser.list.count == 0)
                {
                    self.page = self.page - 1;
                }
                [self.resultsSource addObjectsFromArray:totalUser.list];
                [self.tableView reloadData];
            }
            if (totalUser.last) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
        }
        if(headerRefresh)
        {
            [self.tableView.mj_header endRefreshing];
        }else
        {
            [self.tableView.mj_footer endRefreshing];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.resultsSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MTAddManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MTAddManagerCellID"];
    cell.model = self.resultsSource[indexPath.row];
    cell.delegate = self;
    cell.clipsToBounds = YES;
    return  cell;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MTUserModel *model = self.resultsSource[indexPath.row];
    if([model.userId isEqualToString:[LTClient sharedClient].currentUserId])
    {
        return 0;
    }else{
         return 60;
    }
}

-(void)addMangaerClick:(MTUserModel *)model
{
    [MBProgressHUD showProcessHud:@"" toView:nil];
    [[MTUserApiManager manager] addLiveRoomManager:model.userId completion:^(MTError *mterror) {
         [MBProgressHUD hideHUDForView:nil];
         if(mterror == nil)
         {
             [MBProgressHUD showSuccess:@"添加成功"];
             model.userLiveStatus.isManager = YES;
             [self.tableView reloadData];
             [self.liveVc mtLiveProfileViewSetMamnager:[LTSDKHelper usertransformMTUser:model]];
         }else{
             [MBProgressHUD showError:mterror.errorDescription toView:nil];
         }
    }];
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    if(searchBar.text.length == 0)
    {
        return;
    }
    [[MTUserApiManager manager] searchUserWithKeyWord:self.searchBar.text page:self.page completion:^(MTUserTotalModel *totalUser, MTError *mterror) {
        if(mterror == nil)
        {
            self.resultsSource = totalUser.list;
            [self.tableView reloadData];
            if (totalUser.last) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
        }
    }];
    
}

- (IBAction)CancleButtonClick:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
